﻿#include "vipeditdialog.h"
#include "ui_vipeditdialog.h"
#include <QString>
#include <sqlmanage.h>
#include <QMessageBox>
#include <QPushButton>
VipEditDialog::VipEditDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::VipEditDialog)
{
    ui->setupUi(this);
    qDebug()<<"-->"<<flag;

    disconnect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(on_buttonBox_accepted()));
    //connect(this, SIGNAL(accepted()), this, SLOT(on_buttonBox_accepted()), Qt::UniqueConnection);
}

VipEditDialog::~VipEditDialog()
{
    qDebug()<<"VipEditDialog del";
    delete ui;
}

void VipEditDialog::setEditMode(bool mode)
{
    this->flag = mode;
    qDebug()<<"<-->"<<flag;
}
void VipEditDialog::on_buttonBox_accepted()
{
    ui->lineEdit_phone->setReadOnly(!flag);  //根据模式选择phone是否可编辑
    qDebug()<<tr("test5-启用数据库");
    QString name = ui->lineEdit_name->text();
    QString phone = ui->lineEdit_phone->text();
    QString address = ui->lineEdit_address->text();
    QString grade = ui->comboBox_grade->currentText();
    QString integral = ui->lineEdit_integral->text();
    QString consum = ui->lineEdit_consum->text();
    QString email = ui->lineEdit_email->text();
    QString remarks = ui->textEdit_remarks->toPlainText();

    SqlManage conn;
    if(flag){

        //采用新行插入模式
        QString command = QString("INSERT INTO vip(vip_name, vip_phone, vip_address, vip_grade, vip_integral, consumption, email, vip_remarks) VALUES ('%1', %2, '%3', '%4', %5, %6, '%7', '%8');")
                .arg(name)
                .arg(phone)
                .arg(address)
                .arg(grade)
                .arg(integral)
                .arg(consum)
                .arg(email)
                .arg(remarks);
        qDebug()<<command;
        if(conn.addItem(command)){
            qDebug()<<"insert successful";
        }
        else{
            qDebug()<<"insert failed";
        }
    }
    else{

        //采用修改模式
        QString command = QString("UPDATE vip SET vip_name='%1', vip_phone=%2, vip_address='%3', vip_grade='%4', vip_integral=%5, consumption=%6, email='%7', vip_remarks='%8' WHERE id = %9;")
                .arg(name)
                .arg(phone)
                .arg(address)
                .arg(grade)
                .arg(integral)
                .arg(consum)
                .arg(email)
                .arg(remarks)
                .arg(info.at(0));   //id
        if(conn.editItemById(command)){
            qDebug()<<"edit successful";
        }
        else{
            qDebug()<<"edit failed";
        }
    }
    conn.closeSql();
    this->close();
}

void VipEditDialog::on_buttonBox_clicked(QAbstractButton *button)
{
    if(ui->buttonBox->button(QDialogButtonBox::Ok) ==  button){
        QString name = ui->lineEdit_name->text();
        QString phone = ui->lineEdit_phone->text();
        QString address = ui->lineEdit_address->text();
        QString grade = ui->comboBox_grade->currentText();
        QString integral = ui->lineEdit_integral->text();
        QString consum = ui->lineEdit_consum->text();
        QString email = ui->lineEdit_email->text();
        if(name==""||phone ==""|| address==""||grade==""||integral==""||consum==""||email==""){
            QMessageBox msg(QMessageBox::Warning, tr("警告"), tr("除备注外必填!"), QMessageBox::Yes);
            msg.exec();
        }
        else{
            on_buttonBox_accepted();
        }
    }
    else{
        emit reject();
    }
}

void VipEditDialog::setSelectedItemRow(int value)
{
    selectedItemRow = value;
}

void VipEditDialog::setInfo(QList<QString> p)
{
    for(int i = 0; i < p.size(); i++){
        info.append(p.at(i));
        //qDebug()<<"-->"<<info.at(i);
    }
    ui->lineEdit_name->setText(info.at(1));
    ui->lineEdit_phone->setText(info.at(2));
    ui->lineEdit_address->setText(info.at(3));
    ui->comboBox_grade->setCurrentText(info.at(4));
    ui->lineEdit_integral->setText(info.at(5));
    ui->lineEdit_consum->setText(info.at(6));
    ui->lineEdit_email->setText(info.at(7));
    ui->textEdit_remarks->setText(info.at(8));

}
